package com.xinai.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xinai.util.DBUtil;
import com.xinai.vo.SysDepartment;

import java.util.List;

public class SysDepartmentDao extends BaseDao {

    private static final String TABLE = "sysDepartment";
   public SysDepartmentDao(Context context){
       super(context);
   }
    //查询一条记录
    public SysDepartment findFirst() {
        return callBack(TYPE_READ, new DaoCallBack<SysDepartment>() {
            @Override
            public SysDepartment invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    SysDepartment sysDepartment = new SysDepartment();
                    sysDepartment.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    sysDepartment.setNo(cursor.getString(cursor.getColumnIndex("no")));
                    sysDepartment.setDepartmentName(cursor.getString(cursor.getColumnIndex("departmentName")));

                    return sysDepartment;
                }
                return null;
            }
        });
    }

    public List<SysDepartment> getList(final String parma) {
        return callBack(TYPE_READ, new DaoCallBack<List<SysDepartment>>() {
            @Override
            public List<SysDepartment> invoke(SQLiteDatabase conn) {
                String sql = " select * from sysDepartment where [no] like ? or departmentName like ? ";
                cursor = conn.rawQuery(sql, new String[] {"%" + parma + "%", "%" + parma + "%"});
                if (cursor == null) {
                    return null;
                }
                List<SysDepartment> departments = DBUtil.cursor2VOList(cursor, SysDepartment.class);
                return departments;
            }
        });
    }

    //写入与修改记录
    public int insert(final SysDepartment sysDepartment) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("replace into sysDepartment(id,[no],departmentName) values(?,?,?)",
                        new Object[] {sysDepartment.getId(),sysDepartment.getNo(),sysDepartment.getDepartmentName()});
                return 1;
            }
        });
    }


}
